/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.dao.impl;

import cc.altius.dao.HealthSpringContactDao;
import cc.altius.exception.CouldNotSaveToDataBaseException;
import cc.altius.model.Category;
import cc.altius.model.HealthSpringContact;
import java.util.List;
import javax.sql.DataSource;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author yogesh
 */
@Repository
public class HealthSpringContactDaoImpl implements HealthSpringContactDao {

    @Autowired
    private DataSource dataSource;
    private JdbcTemplate jdbcTemplate;
    private SessionFactory sessionFactory;
    private HibernateTemplate hibernateTemplate;

    @Autowired
    @Transactional
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Autowired
    @Transactional
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }

    @Override
    @Transactional
    public int addHealthSpringContact(HealthSpringContact healthSpringContact) {
        return (Integer) this.hibernateTemplate.save(healthSpringContact);
    }

    @Override
    @Transactional
    public List<HealthSpringContact> getHealthSpringContactList() {
        return this.hibernateTemplate.find("from HealthSpringContact");
    }

    @Override
    @Transactional
    public int updateHealthSpringContact(HealthSpringContact healthSpringContact) {
        Query query;
        Session session = this.sessionFactory.getCurrentSession();
        //Date date = DateUtils.getCurrentDateObject(DateUtils.IST);
        try {
            String hql = "Update "
                    + " HealthSpringContact as amb"
                    + " set "
                    + " amb.name=:name,"
                    + " amb.contact1=:contact1,"
                    + " amb.contact2=:contact2,"
                    + " amb.email=:email,"
                    + " amb.category.categoryId=:categoryId,"
                    + " amb.designation.designationId=:designationId,"
                    + " amb.healthSpringCenter.centerId=:centerId,"
                    + " amb.kinName=:kinName,"
                    + " amb.kinPhone=:kinPhone,"
                    + " amb.kinAddress=:kinAddress"
                    + " WHERE amb.healthSpringContactId=:healthSpringContactId";
            query = session.createQuery(hql);
            query.setParameter("name", healthSpringContact.getName());
            query.setParameter("contact1", healthSpringContact.getContact1());
            query.setParameter("contact2", healthSpringContact.getContact2());
            query.setParameter("email", healthSpringContact.getEmail());
            query.setParameter("categoryId", healthSpringContact.getCategory().getCategoryId());
            query.setParameter("designationId", healthSpringContact.getDesignation().getDesignationId());
            query.setParameter("centerId", healthSpringContact.getHealthSpringCenter().getCenterId());
            //by Santosh
            query.setParameter("kinName", healthSpringContact.getKinName());
            query.setParameter("kinPhone", healthSpringContact.getKinPhone());
            query.setParameter("kinAddress", healthSpringContact.getKinAddress());


            query.setParameter("healthSpringContactId", healthSpringContact.getHealthSpringContactId());
            int rowCount = query.executeUpdate();
            return rowCount;
        } catch (Exception e) {
            throw new CouldNotSaveToDataBaseException("Failed to add Health Spring Contact because" + e.getMessage());
        }
    }

    @Override
    @Transactional
    public List<Category> getCategoryList() {
        return this.hibernateTemplate.find("from Category");
    }

    @Override
    @Transactional
    public HealthSpringContact getHealthSpringContactById(int healthSpringContactId) {
        Session session = this.sessionFactory.getCurrentSession();
        Query query = session.createQuery("from HealthSpringContact where healthSpringContactId=" + healthSpringContactId);
        return (HealthSpringContact) query.uniqueResult();
    }
}
